// #define DEBUG_ENABLE_KUNLUN
#include "group_norm_tiling.h"
#include "register/op_def_registry.h"
// #include "graph/utils/type_utils.h"
// #include "tiling/platform/platform_ascendc.h"
// #include "utils/debug_kl.h"
// #include "utils/tiling_algorithm.h"
namespace optiling
{
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        GroupNormTilingData tiling;
        const gert::StorageShape *x_shape = context->GetInputShape(0);
        int32_t N, C, H, W, dimN;
        int32_t num_groups, num_channels, channels_per_group;
        float eps;
        bool affine;
        dimN = x_shape->GetStorageShape().GetDimNum();
        ge::DataType dataType = context->GetInputTensor(0)->GetDataType();
        if(dataType==ge::DT_FLOAT){
            if (dimN == 2)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = 1;
                W = 1;
                context->SetTilingKey(1);
                // P(dimN);
                // P(dataType);
            }
            if (dimN == 3)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = x_shape->GetStorageShape().GetDim(2);
                W = 1;
                context->SetTilingKey(2);
                // P(dimN);
                // P(dataType);
            }
            if (dimN == 4)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = x_shape->GetStorageShape().GetDim(2);
                W = x_shape->GetStorageShape().GetDim(3);
                context->SetTilingKey(3);
                // P(dimN);
                // P(dataType);
            }
        }
        if(dataType==ge::DT_BF16){
            if (dimN == 2)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = 1;
                W = 1;
                context->SetTilingKey(4);
                // P(dimN);
                // P(dataType);
            }
            if (dimN == 3)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = x_shape->GetStorageShape().GetDim(2);
                W = 1;
                context->SetTilingKey(5);
                // P(dimN);
                // P(dataType);
            }
            if (dimN == 4)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = x_shape->GetStorageShape().GetDim(2);
                W = x_shape->GetStorageShape().GetDim(3);
                context->SetTilingKey(6);
                // P(dimN);
                // P(dataType);
            }
        }
        if(dataType==ge::DT_FLOAT16){
            if (dimN == 2)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = 1;
                W = 1;
                context->SetTilingKey(7);
                // P(dimN);
                // P(dataType);
            }
            if (dimN == 3)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = x_shape->GetStorageShape().GetDim(2);
                W = 1;
                context->SetTilingKey(8);
                // P(dimN);
                // P(dataType);
            }
            if (dimN == 4)
            {
                N = x_shape->GetStorageShape().GetDim(0);
                C = x_shape->GetStorageShape().GetDim(1);
                H = x_shape->GetStorageShape().GetDim(2);
                W = x_shape->GetStorageShape().GetDim(3);
                context->SetTilingKey(9);
                // P(dimN);
                // P(dataType);
            }
        }
        num_groups = *context->GetAttrs()->GetInt(0);
        num_channels = *context->GetAttrs()->GetInt(1);
        channels_per_group = num_channels / num_groups;
        eps = *context->GetAttrs()->GetFloat(0);
        affine = *context->GetAttrs()->GetBool(0);

        tiling.set_num_groups(num_groups);
        tiling.set_num_channels(num_channels);
        tiling.set_channels_per_group(channels_per_group);
        tiling.set_eps(eps);
        tiling.set_affine(affine);
        tiling.set_N(N);
        tiling.set_C(C);
        tiling.set_H(H);
        tiling.set_W(W);
        
        context->SetBlockDim(1);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

        return ge::GRAPH_SUCCESS;
    }
}

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x_shape;
        return GRAPH_SUCCESS;
    }
    static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
    {
        const auto inputDataType = context->GetInputDataType(0);
        context->SetOutputDataType(0, inputDataType);
        return ge::GRAPH_SUCCESS;
    }
}

namespace ops
{
    class GroupNorm : public OpDef
    {
    public:
        explicit GroupNorm(const char *name) : OpDef(name)
        {
            this->Input("x")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
                // .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
                // .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                // .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Output("y")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_BF16})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
                // .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
                // .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                // .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Attr("num_groups").Int();
            this->Attr("num_channels").Int();
            this->Attr("eps").AttrType(OPTIONAL).Float(1e-05);
            this->Attr("affine").AttrType(OPTIONAL).Bool(true);

            this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend910b").AddConfig("ascend310b");
        }
    };

    OP_ADD(GroupNorm);
}